home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Networking / OTEndpointInfo1.0d1 / OTEndpointInfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-02  |  6.7 KB  |  223 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        OTEndpointInfo.c
  3.  
  4.     Contains:    Sample to dump out the endpoint info for common OT endpoint types.
  5.  
  6.     Written by:    Quinn "The Eskimo!"
  7.  
  8.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.     You may incorporate this sample code into your applications without
  13.     restriction, though the sample code has been provided "AS IS" and the
  14.     responsibility for its operation is 100% yours.  However, what you are
  15.     not permitted to do is to redistribute the source as "DSC Sample Code"
  16.     after having made changes. If you're going to re-distribute the source,
  17.     we require that you make it clear in the source that the code was
  18.     descended from Apple Sample Code, but that you've made changes.
  19. */
  20.  
  21. /////////////////////////////////////////////////////////////////////
  22. // The OT debugging macros in <OTDebug.h> require this variable to
  23. // be set.
  24.  
  25. #ifndef qDebug
  26. #define qDebug    1
  27. #endif
  28.  
  29. /////////////////////////////////////////////////////////////////////
  30. // Pick up all the standard OT stuff.
  31.  
  32. #include <OpenTransport.h>
  33.  
  34. /////////////////////////////////////////////////////////////////////
  35. // Pick up device type definitions.
  36.  
  37. #include <OpenTptLinks.h>
  38.  
  39. /////////////////////////////////////////////////////////////////////
  40. // Pick up device type definitions.
  41.  
  42. #include <OpenTptAppleTalk.h>
  43.  
  44. /////////////////////////////////////////////////////////////////////
  45. // Pick up device type definitions.
  46.  
  47. #include <OpenTptInternet.h>
  48.  
  49. /////////////////////////////////////////////////////////////////////
  50. // Pick up the OTDebugBreak and OTAssert macros.
  51.  
  52. #include <OTDebug.h>
  53.  
  54. /////////////////////////////////////////////////////////////////////
  55. // Standard C prototypes.
  56.  
  57. #include <stdio.h>
  58.  
  59. /////////////////////////////////////////////////////////////////////
  60. // OTDebugStr is not defined in any OT header files, but it is
  61. // exported by the libraries, so we define the prototype here.
  62.  
  63. extern pascal void OTDebugStr(const char* str);
  64.  
  65. /////////////////////////////////////////////////////////////////////
  66.  
  67. /*******************************************************************************
  68. ** EndPointInfo
  69. ********************************************************************************/
  70.  
  71. enum {
  72.     kNumberOfServiceTypeNames = 8
  73. };
  74.  
  75. static char *gServiceTypeConstants[kNumberOfServiceTypeNames] = {
  76.     "unknown",
  77.     "T_COTS",
  78.     "T_COTS_ORD",
  79.     "T_CLTS",
  80.     "unknown",
  81.     "T_TRANS",
  82.     "T_TRANS_ORD",
  83.     "T_TRANS_CLTS"
  84. };
  85.  
  86. static char *gServiceTypeDescriptions[kNumberOfServiceTypeNames] = {
  87.     "unknown",
  88.     "Connection-oriented, transactionless",
  89.     "Connection-oriented, transactionless, orderly release",
  90.     "Connectionless, transactionless",
  91.     "unknown",
  92.     "Connection-oriented, transaction",
  93.     "Connection-oriented, transaction, orderly release",
  94.     "Connectionless, transaction"
  95. };
  96.  
  97. static void PrintFlag(char *flagName, UInt32 flagField, UInt32 flagMask)
  98.     // If the flagMask bit in flagField is set, print the flagName
  99.     // string.
  100. {
  101.   printf("    %s = %d\n", flagName, (flagField & flagMask) != 0);
  102. }
  103.  
  104. static void ValueToString(SInt32 value, char *valueString)
  105.     // Convert value to a string and put it in valueString,
  106.     // formatting the two special case values appropriately.
  107. {
  108.     switch (value) {
  109.         case T_INFINITE:
  110.             sprintf(valueString, "T_INFINITE (%ld)", value);
  111.             break;
  112.         case T_INVALID:
  113.             sprintf(valueString, "T_INVALID  (%ld)", value);
  114.             break;
  115.         default:
  116.             sprintf(valueString, "%ld", value);
  117.             break;
  118.     }
  119. }
  120.  
  121. static void OpenEndpointAndPrintInfo(char *config)
  122.     // Open an endpoint with the given configuration, call
  123.     // OTGetEndpointInfo to get the endpoint information,
  124.     // and print that out in a nicely formatted manner.
  125. {
  126.     OSStatus err;
  127.     EndpointRef ep;
  128.     TEndpointInfo epInfo;
  129.     char *serviceTypeConstant;
  130.     char *serviceTypeDescription;
  131.     char valueString[256];
  132.         
  133.     ep = OTOpenEndpoint(OTCreateConfiguration(config), 0, &epInfo, &err);
  134.     if (err == noErr) {
  135.         printf("TEndpointInfo for “%s”\n", config);
  136.         
  137.         ValueToString(epInfo.addr, valueString);
  138.         printf("  addr (max size of address)              = %s\n", valueString);
  139.         
  140.         ValueToString(epInfo.options, valueString);
  141.         printf("  options (max size of options)           = %s\n", valueString);
  142.         
  143.         ValueToString(epInfo.tsdu, valueString);
  144.         printf("  tsdu (max size of data unit)            = %s\n", valueString);
  145.         
  146.         ValueToString(epInfo.etsdu, valueString);
  147.         printf("  etsdu (max size of expedited data unit) = %s\n", valueString);
  148.         
  149.         ValueToString(epInfo.connect, valueString);
  150.         printf("  connect (max size of connect data)      = %s\n", valueString);
  151.         
  152.         ValueToString(epInfo.discon, valueString);
  153.         printf("  discon (max size of disconnect data)    = %s\n", valueString);
  154.         
  155.         if (epInfo.servtype >= 0 && epInfo.servtype < kNumberOfServiceTypeNames) {
  156.             serviceTypeConstant = gServiceTypeConstants[epInfo.servtype];
  157.             serviceTypeDescription = gServiceTypeDescriptions[epInfo.servtype];
  158.         } else {
  159.             serviceTypeConstant = "unknown";
  160.             serviceTypeDescription = "unknown";
  161.         };
  162.         printf("  servtype                                = %s (%ld)\n", serviceTypeConstant, epInfo.servtype);
  163.         printf("    (%s)\n", serviceTypeDescription);
  164.         printf("  flags                                   = $%08x\n", epInfo.flags);
  165.         PrintFlag("T_SENDZERO (can send 0 byte units)        ", epInfo.flags, T_SENDZERO);
  166.         PrintFlag("T_XPG4_1 (supports GetProtAddress)        ", epInfo.flags, T_XPG4_1);
  167.         PrintFlag("CAN_RESOLVE_ADDR (supports ResolveAddress)", epInfo.flags, T_CAN_RESOLVE_ADDR);
  168.         PrintFlag("CAN_SUPPLY_MIB (SNMP)                     ", epInfo.flags, T_CAN_SUPPLY_MIB);
  169.         PrintFlag("T_CAN_SUPPORT_MDATA (support M_DATA mode) ", epInfo.flags, T_CAN_SUPPORT_MDATA);
  170.  
  171.         (void) OTCloseProvider(ep);
  172.     };
  173.     
  174.     if (err != noErr) {
  175.       printf("OpenEndpointAndPrintInfo(%s) = %d\n", config, err);
  176.     };
  177.     printf("\n");
  178. }
  179.  
  180. static void EndpointInfo(void)
  181. {
  182.     OpenEndpointAndPrintInfo(kEnetName);
  183.     
  184.     OpenEndpointAndPrintInfo(kRawIPName);
  185.     OpenEndpointAndPrintInfo(kUDPName);
  186.     OpenEndpointAndPrintInfo(kTCPName);
  187.     
  188.     OpenEndpointAndPrintInfo(kDDPName);
  189.     OpenEndpointAndPrintInfo(kATPName);
  190.     OpenEndpointAndPrintInfo(kPAPName);
  191.     OpenEndpointAndPrintInfo(kADSPName);
  192. }
  193.  
  194. /////////////////////////////////////////////////////////////////////
  195.  
  196. void main(void)
  197. {
  198.     OSStatus err;
  199.     
  200.     printf("OTEndpointInfo\n");
  201.     printf("-- Prints TEndpointInfo for common endpoint configurations.\n");
  202.     printf("-- This is for informational purposes only, don't even think\n");
  203.     printf("-- about hard-coding any of these values!  If you need them you\n");
  204.     printf("-- should fetch them at run time using OTGetEndpointInfo.\n");
  205.     printf("\n");
  206.     
  207.     err = InitOpenTransport();
  208.     
  209.     if (err == noErr) {
  210.     
  211.         EndpointInfo();
  212.         
  213.         CloseOpenTransport();
  214.     }
  215.     
  216.     if (err == noErr) {
  217.         printf("Success.\n");
  218.     } else {
  219.         printf("Failed with error %d.\n", err);
  220.     }
  221.     printf("Done.  Press command-Q to Quit.\n");
  222. }
  223.